package de.tmahr.android;

import de.tmahr.android.logger.LogActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class SensorTest extends LogActivity implements SensorEventListener
{
	private StringBuilder sb = new StringBuilder();
	private SensorManager sensorManager;
	int sensorType = Sensor.TYPE_ACCELEROMETER;
	private Sensor sensor;
	
	public SensorTest()
	{
		super("SensorTest");
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		if(sensorManager.getSensorList(sensorType).size()==0)
		{
			log("Es gibt den Sensor nicht");
			sensor = null;
		}
		else
		{
			sensor = sensorManager.getSensorList(sensorType).get(0);
		}		
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();

		if(sensor != null)
		{
			if(sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME))
			{
				log("Bei Sensor angemeldet.");
			}
			else
			{
				log("Kann dem Sensor keinen Beobachter hinzufügen");
			}
		}		
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		if(sensor != null)
		{
			sensorManager.unregisterListener(this,sensor);
			log("Von Sensor abgemeldet.");
		}		
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event)
	{
		clearLog();
		sb.setLength(0);
		sb.append("x=").append(event.values[0]);
		sb.append("\ny=").append(event.values[1]);
		sb.append("\nz=").append(event.values[2]);
		log(sb.toString());		
	}
}






